﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MouseRayCaster : MonoBehaviour
{
    // Start is called before the first frame update
    private static MouseRayCaster m_Instance = null;
    public static MouseRayCaster Instance {
        get {
            if(m_Instance == null) {
                m_Instance = GameObject.FindObjectOfType<MouseRayCaster>() as MouseRayCaster;
            }
            if(!m_Instance) {
                GameObject obj = new GameObject("MouseRayCaster");
                m_Instance = obj.AddComponent<MouseRayCaster>();
            }
            return m_Instance;
        }
    }
    RaycastHit m_HitInfo;
    GameObject m_HitObject;
    Ray m_MouseRay;
    public bool IsRayCastOpen = true;
    //private bool m_IsHoldingCard = false;
    private CardUI m_DraggingCard = null; 
    private enum MouseState {
        ON_DRAG, ON_SETTLE, IDLE
    }
    private bool m_IsMouseDown = false;
    private MouseState m_State = MouseState.IDLE;
    private BoardPlacer m_CurrBoardPlacer = null;
    void Start()
    {
        
    }
    /* public void ResetHolding() {
        m_State = MouseState.IDLE;
        DraggingCard = null;
    }
    public void SetHolding(CardProps.ID id) {
        HoldingCardID = id; 
        m_State = MouseState.ON_DRAG;
    }
    public void ReShowCardUI() {
        m_HitObject.GetComponent<CardUI>().ReShow();
    }*/
    private void ResetState() {
        m_State = MouseState.IDLE;
        if(m_CurrBoardPlacer != null) 
            m_CurrBoardPlacer.ClearState();
        m_DraggingCard.DragEnd();
        m_CurrBoardPlacer = null;
        m_DraggingCard = null;
        m_IsMouseDown = false; 
    }
    private void ReleaseSettle() {
        m_State = MouseState.IDLE;
        VSController.Instance.Settle(m_DraggingCard, m_CurrBoardPlacer);
        //m_DraggingCard.SettleDestroy();
        m_CurrBoardPlacer = null;
        m_DraggingCard = null;
        m_IsMouseDown = false; 
    }

    // Update is called once per frame
    void Update() {
        m_IsMouseDown = Input.GetMouseButton(0);
        if(!IsRayCastOpen) return;
        if(m_State != MouseState.IDLE && Input.GetMouseButtonUp(0)) {
            if(m_State == MouseState.ON_SETTLE)
                ReleaseSettle();
            else ResetState();
            return;
        }
        m_MouseRay = Camera.main.ScreenPointToRay(Input.mousePosition);
        if (Physics.Raycast(m_MouseRay, out m_HitInfo)) {
            //Debug.Log("hit!");
            //划出射线，只有在scene视图中才能看到
            Debug.DrawLine(m_MouseRay.origin, m_HitInfo.point);
            m_HitObject = m_HitInfo.collider.gameObject;
            switch(m_HitObject.tag) {
                case "HandCard":
                if(m_State == MouseState.IDLE && m_IsMouseDown) {
                    m_DraggingCard = m_HitObject.GetComponent<CardUI>(); 
                    if(!VSController.Instance.CheckCanSettle(m_DraggingCard.HandIndex)) {
                        Debug.LogError(("卡牌费用不足"));
                        break;
                    }
                    m_DraggingCard.OnMouseRayHit();
                    m_State = MouseState.ON_DRAG;
                }
                    break;
                case "BoardPlacer":
                    //Debug.Log("BP hit");
                    if(m_State == MouseState.ON_DRAG) {
                        m_DraggingCard.Hide();
                        m_State = MouseState.ON_SETTLE;
                        m_CurrBoardPlacer = m_HitObject.GetComponent<BoardPlacer>();
                        m_CurrBoardPlacer.OnMouseRayHit(m_DraggingCard.CurrCardID);
                        break;
                    }
                    else if(m_State == MouseState.ON_SETTLE) {
                        BoardPlacer newBoardPlacer = m_HitObject.GetComponent<BoardPlacer>();
                        if(!ReferenceEquals(newBoardPlacer,m_CurrBoardPlacer)) {
                            m_CurrBoardPlacer.ClearState();
                        }
                        m_CurrBoardPlacer = newBoardPlacer;
                        m_CurrBoardPlacer.OnMouseRayHit(m_DraggingCard.CurrCardID);
                    }
                    break;
                default:
                    break;
            }
        }
        //hit nothing. 
        else {
            if(m_State == MouseState.ON_SETTLE) {
                m_State = MouseState.ON_DRAG;
                m_CurrBoardPlacer.ClearState();
                m_CurrBoardPlacer = null;
                m_DraggingCard.ReShow();
            }
        }
    }
}
